package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.SearchSongRoot;
import com.lanou.moving.utils.Exchange;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class SongListViewAdapter extends BaseAdapter {
    private Context mcontext;
    private List<SearchSongRoot.DataEntity> data;
    private OnMenuClickListener onMenuClickListener;

    public SongListViewAdapter(Context c, List<SearchSongRoot.DataEntity> s) {
        mcontext = c;
        data = s;
    }

    public interface OnMenuClickListener {
        void onMenuClick(SearchSongRoot.DataEntity songsEntity);
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.songs_list_listview_item, null);
            holder = new ViewHolder();
            holder.songs_list_position = (TextView) convertView.findViewById(R.id.songs_list_position);
            holder.songs_list_songName = (TextView) convertView.findViewById(R.id.songs_list_songName);
            holder.songs_list_item_singer = (TextView) convertView.findViewById(R.id.songs_list_item_singer);
            holder.songs_list_item_number = (TextView) convertView.findViewById(R.id.songs_list_item_number);
            holder.list_more = (ImageView) convertView.findViewById(R.id.list_more);
            holder.list_more.setVisibility(View.GONE);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SearchSongRoot.DataEntity entity = (SearchSongRoot.DataEntity) getItem(position);

        if (entity != null) {
            String num = Exchange.getCount(entity.getFavorites());
            holder.songs_list_position.setText(position + 1 + "");
            holder.songs_list_songName.setText(entity.getName());
            holder.songs_list_item_singer.setText(entity.getSingerName());
            holder.songs_list_item_number.setText(num);
        }

        return convertView;
    }

    private class ViewHolder {
        private ImageView list_more;
        private TextView songs_list_position;
        private TextView songs_list_songName;
        private TextView songs_list_item_singer;
        private TextView songs_list_item_number;

    }

}